﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using Hfm.Common.Entity.Contacts;
using Hfm.Common.Entity.Customer;
using Hfm.Core.DataAccess.Customer;

namespace Hfm.Core.BusinessManager.Customer
{
    public class CustomerManager
    {
        protected readonly CustomerData CustomerDataAccess;
        protected readonly ContactsData ContactsDataAccess;

        public CustomerManager()
        {
            
        }

        public CustomerManager(CustomerData customerData,ContactsData contactsdata)
        {
            CustomerDataAccess = customerData;
            ContactsDataAccess = contactsdata;
        }
        /// <summary>
        /// 根据Id查询Customer
        /// </summary>
        /// <param name="CustomerId"></param>
        /// <returns></returns>
        public virtual CustomerEntity GetCustomerById(int CustomerId)
        {
            return CustomerDataAccess.GetCustomerById(CustomerId);
        }
        /// <summary>
        /// 根据Id查询Customer
        /// </summary>
        /// <param name="CustomerId"></param>
        /// <returns></returns>
        public virtual NewCustomerEntity GetNewCustomerById(int CustomerId)
        {
            return CustomerDataAccess.GetNewCustomerById(CustomerId);
        }
        /// <summary>
        /// 根据顾客名字或编号查询
        /// </summary>
        /// <param name="CustomerName"></param>
        /// <param name="CustomerCode"></param>
        /// <returns></returns>
        public virtual IList<NewCustomerEntity> GetCustomerByParameter(string CustomerName = null, string CustomerCode = null,int? skip = null, int? take = null)
        {
            return CustomerDataAccess.GetCustomerByParameter(CustomerName, CustomerCode,skip,take);
        }

        /// <summary>
        /// 通过code查找顾客
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public virtual CustomerEntity GetCustomerByCode(string code)
        {
            return CustomerDataAccess.GetCustomerByCode(code);
        }

        /// <summary>
        /// 查询所有顾客
        /// </summary>
        /// <returns></returns>
        public virtual IList<NewCustomerEntity> GetCustomers(int? skip = null, int? take = null)
        {
            return CustomerDataAccess.GetCustomers(skip,take);
        }

        /// <summary>
        /// 查询所有已激活顾客
        /// </summary>
        /// <returns></returns>
        public virtual IList<NewCustomerEntity> GetCustomerByStatus()
        {
            return CustomerDataAccess.GetCustomerByStatus();
        }

        /// <summary>
        /// 创建顾客
        /// </summary>
        /// <param name="Customer"></param>
        public virtual void CreateCustomer(CustomerEntity Customer)
        {
            CustomerDataAccess.CreateCustomer(Customer);
        }
        /// <summary>
        /// 创建顾客与联系人
        /// </summary>
        /// <param name="Customer"></param>
        /// <param name="Contacts"></param>
        public virtual void CreateCustomerAndContacts(CustomerEntity Customer,IList<ContactsEntity> Contacts )
        {
            using (TransactionScope Scope=new TransactionScope())
            {
                CustomerDataAccess.CreateCustomer(Customer);
                if (Contacts!=null)
                {
                    foreach (ContactsEntity newContacts in Contacts)
                    {
                        newContacts.CID = Customer.Id;
                        ContactsDataAccess.CreateContacts(newContacts);
                    }
                }
                Scope.Complete();
            }
            
        }
        /// <summary>
        /// 修改顾客
        /// </summary>
        /// <param name="Customer"></param>
        public virtual void UpdateCustomer(CustomerEntity Customer)
        {
            CustomerDataAccess.UpdateCustomer(Customer);
        }

        public virtual void UpdateCustomerAndContacts(CustomerEntity Customer, IList<ContactsEntity> Contacts)
        {
            using (TransactionScope Scope=new TransactionScope())
            {
                CustomerDataAccess.UpdateCustomer(Customer);
                IList<ContactsEntity> contactsList = ContactsDataAccess.GetContactsByCustomerID(Customer.Id);

                if (Contacts!=null)
                {
                    foreach (ContactsEntity contacts in Contacts)
                    {
                        if (contacts.ID==0)
                        {
                            contacts.CID = Customer.Id;
                            ContactsDataAccess.CreateContacts(contacts);
                        }
                    }

                    foreach (ContactsEntity oldContacts in contactsList)
                    {
                        bool result = false;
                        foreach (ContactsEntity newContacts in Contacts)
                        {
                            if (oldContacts.ID == newContacts.ID)
                            {
                                oldContacts.FullName = newContacts.FullName;
                                oldContacts.OfficeNumber = newContacts.OfficeNumber;
                                oldContacts.Mobile = newContacts.Mobile;
                                oldContacts.FaxNumber = newContacts.FaxNumber;
                                oldContacts.Email = newContacts.Email;
                                oldContacts.Designation = newContacts.Designation;
                                oldContacts.SalutationID = newContacts.SalutationID;
                                oldContacts.Department = newContacts.Department;
                                oldContacts.Remarks = newContacts.Remarks;
                                oldContacts.CID = Customer.Id;

                                ContactsDataAccess.UpdateContacts(oldContacts);
                                result = true;
                                break;
                            }
                        }
                        if (result == false)
                        {
                            ContactsDataAccess.Context.ContactsEntities.Remove(oldContacts);
                            ContactsDataAccess.Context.SaveChanges();
                        }
                    }
                }
                Scope.Complete();
            }
            
        }
        /// <summary>
        /// 删除顾客
        /// </summary>
        /// <param name="ID"></param>
        public virtual void DeleteCustomer(int ID)
        {
            CustomerDataAccess.DeleteCustomer(ID);
        }
        /// <summary>
        /// 删除顾客及联系人
        /// </summary>
        /// <param name="ID"></param>
        public virtual void DeleteCustomerAndContacts(int ID)
        {
            CustomerDataAccess.DeleteCustomer(ID);
            ContactsDataAccess.DeleteContactsByCID(ID);
        }
    }
}
